package com.elegantmall.controller;
import cn.dev33.satoken.util.SaResult;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.elegantmall.dao.UserDao;
import com.elegantmall.pojo.UserOrderPojo;
import com.elegantmall.service.UserOrderServiceImpl;
import com.elegantmall.service.UserServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;

@RestController
@RequestMapping("/api/userOrder")
public class UserOrderController {

    @Autowired
    private UserOrderServiceImpl userOrderService;
    @Autowired
    private UserDao userDao;
    // 获取用户所有订单
    @GetMapping("/list")
    public SaResult getUserOrderList(
            @RequestParam String username) {

        try {
            List<UserOrderPojo> orderList = userOrderService.getOrdersByUsername(username);
            return SaResult.data(orderList);
        } catch (IllegalArgumentException e) {
            return SaResult.error(e.getMessage());
        } catch (Exception e) {
            return SaResult.error("获取订单列表失败");
        }
    }

    // 创建新订单
    @PostMapping("/create")
    public SaResult createUserOrder(
            @RequestParam String username,
            @RequestParam String orderid,
            @RequestParam String seller,
            @RequestParam String goods) {
        try {
            UserOrderPojo newOrder = new UserOrderPojo();
            newOrder.setUsername(username);
            newOrder.setOrderid(orderid);
            newOrder.setSeller(seller);
            newOrder.setGoods(goods);

            String result = userOrderService.createUserOrder(username,orderid,seller,goods);
            return SaResult.data(result);
        } catch (Exception e) {
            return SaResult.error("订单创建异常: " + e.getMessage());
        }
    }

    // 删除订单
    @DeleteMapping("/delete")
    public SaResult deleteUserOrder(
            @RequestParam String username,
            @RequestParam String orderid) {
        try {
            String result = userOrderService.deleteUserOrder(username,orderid);
            return SaResult.ok(result);
        } catch (Exception e) {
            return SaResult.error("订单删除异常: " + e.getMessage());
        }
    }

    /*// 可选：更新订单信息
    @PutMapping("/update")
    public SaResult updateUserOrder(
            @RequestBody UserOrderPojo order) {
        try {
            boolean result = userOrderService.updateById(order);
            return result ? SaResult.ok("订单更新成功") : SaResult.error("订单更新失败");
        } catch (Exception e) {
            return SaResult.error("订单更新异常: " + e.getMessage());
        }
    }*/
}